
package za.ac.cput.peterjohn.DesignPatternPrincipals.Behavioral.Interpreter.Model;

import za.ac.cput.peterjohn.DesignPatternPrincipals.Behavioral.Interpreter.Model.DivideExpression;
import java.util.Map;
import java.util.Stack;

public class Evaluator implements Expression
{
    private Expression syntaxTree;

    public Evaluator(String expression)
    {
        Stack<Expression> numbers = new Stack<Expression>();
        
            for (String token: expression.split(" "))
            {
                if(token.equals("*"))
                {
                    Expression times = new MulitplyExpression(numbers.pop(), numbers.pop());
                    numbers.push(times);
                    
                }else if(token.equals("/"))
                {    
                    Expression right = numbers.pop();
                    Expression left = numbers.pop();
                    Expression divide = new DivideExpression(left, right);
                    numbers.push(divide);
                }
                else
                    numbers.push(new Varible(token));     
            }
            
            syntaxTree = numbers.pop();
                    
    }
    
    @Override
    public int interpret(Map<String, Expression> Varibles){ return syntaxTree.interpret(Varibles); }  
}
